<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MSSQL to MySQL 数据库迁移工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .main-content {
            padding: 40px;
        }

        .database-forms {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 40px;
            margin-bottom: 40px;
        }

        @media (max-width: 768px) {
            .database-forms {
                grid-template-columns: 1fr;
                gap: 30px;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .main-content {
                padding: 20px;
            }
        }

        .form-section {
            background: #f8f9fa;
            padding: 30px;
            border-radius: 10px;
            border: 2px solid #e9ecef;
        }

        .form-section h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
        }

        .form-section h2::before {
            content: '';
            width: 20px;
            height: 20px;
            margin-right: 10px;
            border-radius: 3px;
        }

        .mssql-section h2::before {
            background: #ff6b6b;
        }

        .mysql-section h2::before {
            background: #4ecdc4;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #495057;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #dee2e6;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .migrate-section {
            text-align: center;
            padding: 30px;
            background: #f8f9fa;
            border-radius: 10px;
            margin-bottom: 30px;
        }

        .migrate-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 40px;
            font-size: 1.2rem;
            font-weight: 600;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }

        .migrate-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
        }

        .migrate-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .status-section {
            background: #e3f2fd;
            border: 2px solid #2196f3;
            border-radius: 10px;
            padding: 20px;
            margin-top: 20px;
            display: none;
        }

        .status-section.show {
            display: block;
        }

        .status-section h3 {
            color: #1976d2;
            margin-bottom: 15px;
        }

        .status-message {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 10px;
        }

        .status-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .status-info {
            background: #cce7ff;
            color: #004085;
            border: 1px solid #b3d7ff;
        }

        .admin-link {
            position: fixed;
            top: 20px;
            right: 20px;
            background: rgba(0,0,0,0.7);
            color: white;
            padding: 10px 20px;
            border-radius: 25px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        .admin-link:hover {
            background: rgba(0,0,0,0.9);
            transform: scale(1.05);
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <a href="/admin" class="admin-link">管理后台</a>
    
    <div class="container">
        <div class="header">
            <h1>MSSQL to MySQL 迁移工具</h1>
            <p>快速、安全地将您的MSSQL数据库迁移到MySQL</p>
        </div>
        
        <div class="main-content">
            <form id="migrationForm">
                <div class="database-forms">
                    <div class="form-section mssql-section">
                        <h2>MSSQL 源数据库配置</h2>
                        <div class="form-group">
                            <label for="mssql_host">主机IP地址</label>
                            <input type="text" id="mssql_host" name="mssql_host" required placeholder="例如: 192.168.1.100">
                        </div>
                        <div class="form-group">
                            <label for="mssql_database">数据库名称</label>
                            <input type="text" id="mssql_database" name="mssql_database" required placeholder="例如: MyDatabase">
                        </div>
                        <div class="form-group">
                            <label for="mssql_port">端口号</label>
                            <input type="number" id="mssql_port" name="mssql_port" value="1433" required>
                        </div>
                        <div class="form-group">
                            <label for="mssql_username">用户名</label>
                            <input type="text" id="mssql_username" name="mssql_username" required placeholder="数据库用户名">
                        </div>
                        <div class="form-group">
                            <label for="mssql_password">密码</label>
                            <input type="password" id="mssql_password" name="mssql_password" required placeholder="数据库密码">
                        </div>
                    </div>
                    
                    <div class="form-section mysql-section">
                        <h2>MySQL 目标数据库配置</h2>
                        <div class="form-group">
                            <label for="mysql_host">主机IP地址</label>
                            <input type="text" id="mysql_host" name="mysql_host" required placeholder="例如: 192.168.1.101">
                        </div>
                        <div class="form-group">
                            <label for="mysql_database">数据库名称</label>
                            <input type="text" id="mysql_database" name="mysql_database" required placeholder="例如: MyNewDatabase">
                        </div>
                        <div class="form-group">
                            <label for="mysql_port">端口号</label>
                            <input type="number" id="mysql_port" name="mysql_port" value="3306" required>
                        </div>
                        <div class="form-group">
                            <label for="mysql_username">用户名</label>
                            <input type="text" id="mysql_username" name="mysql_username" required placeholder="数据库用户名">
                        </div>
                        <div class="form-group">
                            <label for="mysql_password">密码</label>
                            <input type="password" id="mysql_password" name="mysql_password" required placeholder="数据库密码">
                        </div>
                    </div>
                </div>
                
                <div class="migrate-section">
                    <button type="submit" class="migrate-btn" id="migrateBtn">
                        确认迁移
                    </button>
                </div>
            </form>
            
            <div class="status-section" id="statusSection">
                <h3>迁移状态</h3>
                <div id="statusMessages"></div>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('migrationForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const btn = document.getElementById('migrateBtn');
            const statusSection = document.getElementById('statusSection');
            const statusMessages = document.getElementById('statusMessages');
            
            // 禁用按钮并显示加载状态
            btn.disabled = true;
            btn.innerHTML = '<span class="loading"></span>正在迁移...';
            
            // 显示状态区域
            statusSection.classList.add('show');
            statusMessages.innerHTML = '';
            
            // 收集表单数据
            const formData = {
                mssql: {
                    host: document.getElementById('mssql_host').value,
                    database: document.getElementById('mssql_database').value,
                    port: parseInt(document.getElementById('mssql_port').value),
                    username: document.getElementById('mssql_username').value,
                    password: document.getElementById('mssql_password').value
                },
                mysql: {
                    host: document.getElementById('mysql_host').value,
                    database: document.getElementById('mysql_database').value,
                    port: parseInt(document.getElementById('mysql_port').value),
                    username: document.getElementById('mysql_username').value,
                    password: document.getElementById('mysql_password').value
                }
            };
            
            try {
                const response = await fetch('/api/migrate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    addStatusMessage('success', result.message);
                    // 可以在这里添加轮询检查迁移状态的逻辑
                } else {
                    addStatusMessage('error', result.message || '迁移失败');
                }
            } catch (error) {
                addStatusMessage('error', '网络错误: ' + error.message);
            } finally {
                // 恢复按钮状态
                btn.disabled = false;
                btn.innerHTML = '确认迁移';
            }
        });
        
        function addStatusMessage(type, message) {
            const statusMessages = document.getElementById('statusMessages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `status-message status-${type}`;
            messageDiv.textContent = new Date().toLocaleTimeString() + ': ' + message;
            statusMessages.appendChild(messageDiv);
            statusMessages.scrollTop = statusMessages.scrollHeight;
        }
    </script>
</body>
</html>